/**
 * 
 */
package br.com.ctm.advogadope.mobile;

import java.util.Iterator;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.PersistenceException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.apache.commons.lang.exception.ExceptionUtils;

import br.com.ctm.advogadope.model.Advogado;
import br.com.ctm.advogadope.model.Usuario;
import br.com.ctm.advogadope.negocio.api.AdvogadoNegocio;
import br.com.framework.apresentacao.bean.BaseBean;
import br.com.framework.negocio.exception.NegocioException;

/**
 * @author Cleber
 * 
 */
@Named
@ConversationScoped
public class AdvogadoMobileBean extends BaseBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Advogado advogado;
	private Usuario usuario;
	
	@Inject
	private Conversation conversation;
	
	@Inject
	private AdvogadoNegocio advogadoNegocio;

	/**
	 * 
	 */
	public AdvogadoMobileBean() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * Inicia uma conversação.
	 */
	public void initConversation() {
		if (conversation.isTransient()) {
			conversation.begin();
		}
		advogado = new Advogado();
		usuario = new Usuario();
	}

	/**
	 * @return the advogado
	 */
	public Advogado getAdvogado() {
		return advogado;
	}

	/**
	 * @param advogado the advogado to set
	 */
	public void setAdvogado(Advogado advogado) {
		this.advogado = advogado;
	}

	/**
	 * @return the usuario
	 */
	public Usuario getUsuario() {
		return usuario;
	}

	/**
	 * @param usuario the usuario to set
	 */
	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}
	
	/**
	 * @return
	 */
	public String cadastrar() {
		String outcome = "cadastro";
		try {
			usuario.setCliente(advogado);
			advogado.setUsuario(usuario);
			advogadoNegocio.insert(advogado);
			outcome = "pm:cadastroSucesso";
			conversation.end();
		} catch (PersistenceException e) {
			getFacesMessages().addMessageError(null, "advogado.erro.persistencia.inserir");
			LOGGER.error("Falha de persistência ao inserir Advogado", e);
		} catch (NegocioException e) {
			getFacesMessages().addMessageError(null, e.getMessage());
			LOGGER.error("Falha de negocio ao inserir Advogado ", e);
		} catch (Exception e) {
			Throwable rootCause = ExceptionUtils.getRootCause(e);
			if (rootCause != null && rootCause instanceof ConstraintViolationException) {
				Iterator<ConstraintViolation<?>> constraintIt = 
						((ConstraintViolationException)rootCause).getConstraintViolations().iterator();
				while (constraintIt.hasNext()) {
					ConstraintViolation<?> violation = constraintIt.next();
					getFacesMessages().addMessageError(violation.getPropertyPath().toString(), violation.getMessage());
				}
			} else {
				LOGGER.error("Erro ao inserir Advogado", e);
				getFacesMessages().addMessageError(null, rootCause.getMessage());
			}
		}
		return outcome;
	}

}
